library(dplyr)
library(knitr)
library(maptools)
library(rgdal)
library(TreeSegmentation)
library(sp)
library(ggplot2)
library(rgl)
library(clue)
library(lidR)
knit_hooks$set(webgl = hook_webgl)
opts_chunk$set(warning=F,message=F)

#set color ramp for treeID
col = pastel.colors(200)

1 Load in ground-truth

shps<-list.files("/Users/ben/Dropbox/Weecology/ITCs/",pattern=".shp",full.names = T)
itcs<-lapply(shps,readOGR,verbose=F)

names(itcs)<-sapply(itcs,function(x){
  id<-unique(x$Plot_ID)
  x@proj4string<-CRS("+init=epsg:32617")
  return(x)
  })

2 Example Pipeline

2.1 Read in Data

ground_truth<-itcs[[20]]
fname<-get_tile_filname(ground_truth,basename = "2014_OSBS_1_",extension = "_colorized.laz")
tile<-readLAS(paste("../data/2015/cropped_",fname,sep=""))
tile@crs<-CRS("+init=epsg:32617")
plot(tile)

You must enable Javascript to view this page properly.

2.2 Confirm overlap

plot(extent(tile),col='red')
plot(extent(ground_truth),col='blue',add=T)

2.3 Compute Segmentation

silva<-silva2016(tile=tile,extra=T)
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   2.177   0.039   2.241 
## [1] "Creating tree polygons"
dalponte<-dalponte2016(tile=tile,extra=T)
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   2.092   0.024   2.137 
## [1] "Creating tree polygons"
li<-li2012(tile=tile,extra=T)
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   0.122   0.002   0.126 
## [1] "Creating tree polygons"
watershed<-watershed(tile=tile,extra=T)
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   2.274   0.030   2.331 
## [1] "Creating tree polygons"

2.4 View 3d segmentation

plot(silva$tile,color="treeID",col=col)

You must enable Javascript to view this page properly.

2.4.1 Overlay ground truth and predictions

plot(ground_truth,col='red')
plot(silva$convex,add=T)

#plot(dalponte$convex,add=T)

2.4.2 CHM versus predicted polygons

chm=canopy_model(silva$tile)
plot(chm,ext=extent(ground_truth))
plot(ground_truth,add=T,col='red')
plot(silva$convex,add=T)

Okay that’s not great, but let’s keep going for the moment.

2.4.3 Compare Methods

Silva v Dalponte

plot(silva$convex)
plot(dalponte$convex,add=T,col=rgb(0,0,255,20,maxColorValue=255))

Li versus watershed

plot(li$convex)
plot(watershed$convex,add=T,col=rgb(0,0,255,20,maxColorValue=255))

## Assign Trees

Each tree is assigned based on the maximum overlap. Pairwise membership is done using a Hungarian Algorithm. See clue::solve_LSAP.

assignment<-assign_trees(ground_truth,prediction=silva$convex)

2.5 Calculate Intersection over union

#loop through assignments and get jaccard statistic for each assignment pair
statdf<-calc_jaccard(assignment=assignment,ground_truth = ground_truth,prediction=silva$convex)
ggplot(statdf) + geom_histogram(aes(IoU)) + labs(x="Intersection over union") + theme_bw()

mean(statdf$IoU)
## [1] 0.1792981
median(statdf$IoU)
## [1] 0.1639482

3 As a wrapper for one tile, multiple methods, and consensus

results<-evaluate(ground_truth=itcs[[1]],algorithm = c("silva","dalponte","li"),path_to_tiles="/Users/ben/Dropbox/Weecology/NEON/cropped_",compute_consensus = T)
## [1] "silva"
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   2.388   0.016   2.420 
## [1] "Creating tree polygons"
## [1] "Dalpone"
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   2.245   0.022   2.296 
## [1] "Creating tree polygons"
## [1] "li"
## [1] "Computing Ground Model"
## [1] "Computing Canopy Model"
## [1] "Clustering Trees"
##    user  system elapsed 
##   0.199   0.004   0.204 
## [1] "Creating tree polygons"
## [1] "consensus"
ggplot(results,aes(x=IoU,fill=Method)) + geom_histogram(position = position_dodge()) + theme_bw()

results %>% group_by(Method) %>% summarize(mean=mean(IoU),median=median(IoU))
## # A tibble: 4 x 3
##   Method      mean median
##   <chr>      <dbl>  <dbl>
## 1 consensus 0.0396 0.0223
## 2 dalponte  0.132  0.0447
## 3 li        0.348  0.303 
## 4 silva     0.140  0.0440

4 Parallel Wrapper across all tiles

system.time(results_all<-evaluate_all(itcs=itcs,algorithm = c("dalponte","silva","li"),path_to_tiles="/Users/ben/Dropbox/Weecology/NEON/cropped_",cores=4,extra=F,compute_consensus=F))
ggplot(results_all,aes(x=IoU,fill=Method)) + geom_histogram(position = position_dodge()) + theme_bw()
results_all %>% group_by(Method) %>% summarize(mean=mean(IoU),median=median(IoU))